package com.yoomiito.app.ui.my;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.scrat.app.selectorlibrary.activity.ImagePreviewActivity;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.adapter.cicle.CicleGoodRemmendAdapter;
import com.yoomiito.app.base.App;
import com.yoomiito.app.model.cicle.CicleRecommendInfo;
import com.yoomiito.app.present.my.activity.MyPublishMaterialPresenter;
import com.yoomiito.app.ui.RecyclerViewBaseActivity;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.L;
import com.yoomiito.app.utils.wx.WXShareMultiImageHelper;
import com.yoomiito.app.utils.wx.UMengShareUtils;
import com.yoomiito.app.widget.dialog.ShareDialog;
import com.yoomiito.app.widget.dialog.ShareBottomDialog_1;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 宣传素材 商学院界面
 */
public class MyPublishMaterialActivity extends RecyclerViewBaseActivity<MyPublishMaterialPresenter> {
    private CicleGoodRemmendAdapter mAdapter;
    private List<CicleRecommendInfo.DataBean> mList;
    private int mType;
    private ShareBottomDialog_1 shareBottomDialog_1;
    private ShareDialog shareDialog;
    private CicleRecommendInfo.DataBean dataBean;
    @Override
    public MyPublishMaterialPresenter newP() {
        return new MyPublishMaterialPresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mType = getIntent().getIntExtra(Constant.TYPE, Constant.COMMUNITY_TYPE_INVITE_MATERIAL);
        switch (mType) {
            case Constant.COMMUNITY_TYPE_INVITE_MATERIAL:
                mTitleView.setCenterText("宣传素材");
                break;
            case Constant.COMMUNITY_TYPE_TRADE_COLLEAGE:
                mTitleView.setCenterText("商学院");
                break;
        }
        initView();

    }

    @Override
    protected void getNewData() {
        getP().getPublishMaterialData(mType, currentPage);
    }

    @Override
    public String getEmptyContent() {
        return "暂未发布内容";
//        return mType==Constant.COMMUNITY_TYPE_INVITE_MATERIAL?"暂未发布内容":"暂未发布内容";
    }

    int shareType;

    private void initView() {
        mList = new ArrayList<>();

        mAdapter = new CicleGoodRemmendAdapter(null);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            dataBean = mAdapter.getData().get(position);
            switch (view.getId()) {
                case R.id.tv_share:
                    share();
                    break;
                case R.id.iv_photo:
                    ImagePreviewActivity.show(MyPublishMaterialActivity.this, 0, (ArrayList<String>) dataBean.getContent_img(), position);
                    break;
            }


        });
    }

    private void share() {
        if (dataBean.getContent_img() == null || dataBean.getContent_img().size() == 0) {
            AllUtils.copyText(MyPublishMaterialActivity.this, dataBean.getContent());
            Toast.makeText(context, "复制成功", Toast.LENGTH_SHORT).show();
        } else {
            if (shareBottomDialog_1 == null) {
                shareBottomDialog_1 = new ShareBottomDialog_1(MyPublishMaterialActivity.this);
                shareBottomDialog_1.setOnShareItemClickListener(type -> {
                    switch (type) {
                        case 1:
                            shareType = 1;
                            if (Build.VERSION.SDK_INT >= 23) {
                                requestPermission();
                            } else {
                                wxShare(dataBean);
                            }
                            break;
                        case 2:
                            shareType = 2;
                            AllUtils.copyText(MyPublishMaterialActivity.this, dataBean.getContent());
                            requestPermission();
                            if (shareDialog == null) {
                                shareDialog = new ShareDialog(this);
                            }
                            shareDialog.show();
                            break;
                        case 3:
                            shareType = 3;
//                            if (dataBean.getContent_img()==null||dataBean.getContent_img().size()==0){
//                                YMToast.showText("没有图片可以保存");
//                                return;
//                            }
                            requestPermission();
                            break;
                    }
                    shareBottomDialog_1.dismiss();
                });
            }
            shareBottomDialog_1.show();
        }

    }

    private void wxShare(CicleRecommendInfo.DataBean dataBean) {
        List<String> urls = dataBean.getContent_img();
        List<Bitmap> bitmaps = new ArrayList<>();
        if (urls == null || urls.size() == 0) {
//            WXShareMultiImageHelper.shareToSession(this,bitmaps , dataBean.getContent());
            UMengShareUtils.shareWXText(this, dataBean.getContent());
        } else {
            Observable.fromIterable(urls)
                    .subscribeOn(Schedulers.io())
                    .map(url -> Glide.with(MyPublishMaterialActivity.this).asBitmap().load(url).submit().get())
                    .map(bitmap -> {
                        bitmaps.add(bitmap);
                        return bitmaps;
                    })
                    .filter(bitmaps1 -> bitmaps1.size() == urls.size() ? true : false)
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .subscribe(bitmaps1 -> WXShareMultiImageHelper.shareToSession(this, bitmaps, dataBean.getContent()));
        }
    }

    public void setPublishMaterialData(CicleRecommendInfo data) {

        if (currentPage == 1) {
            if (data != null && data.getData() != null) {
                mAdapter.setNewData(data.getData());
                mSmartRefreshLayout.finishRefresh(true);
                mStatusManager.showContent();
            } else {
                mStatusManager.showEmpty();
            }
        } else {
            mSmartRefreshLayout.finishLoadMore(true);
            if (data.getData() == null || data.getData().size() == 0) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                mAdapter.addData(data.getData());
            }
        }
    }

    public void finishRefresh() {
        mSmartRefreshLayout.finishRefresh(true);
        mSmartRefreshLayout.finishLoadMore(true);
    }

    public void requestPermission() {
        AllUtils.requestPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(aBoolean -> {
                    if (shareType == 1) {
                        wxShare(dataBean);
                    } else {
                        L.i("SS: "+aBoolean);
                        if (aBoolean) loadPic(dataBean);
                    }
                });
    }


    private void loadPic(CicleRecommendInfo.DataBean dataBean) {
        if (dataBean.getContent_img() != null && dataBean.getContent_img().size() > 0) {
            ImageLoader.getInstance().downloadPic(MyPublishMaterialActivity.this, dataBean.getContent_img());
            Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show();
        }
    }


}
